package com.example.administrator.x3picturetransliteration.Home.Presenter.Fragment;

import android.content.Context;
import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Base.HttpGo;
import com.example.administrator.x3picturetransliteration.Bean.CarouselListBean;
import com.example.administrator.x3picturetransliteration.Bean.HttpDataBean;
import com.example.administrator.x3picturetransliteration.Dao.Rx.UserRx;
import com.example.administrator.x3picturetransliteration.Home.Activity.SignInActivity;
import com.example.administrator.x3picturetransliteration.Util.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 我的
 * Created by Administrator on 2019/9/9.
 */

public class MePresenter extends BasePresenter{

    public MePresenter(Context context, CompositeSubscription mCompositeSubscription) {
        super(context, mCompositeSubscription);
    }

    /** 获取用户信息 */
    public void info(final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user/info", new HashMap<String, String>(),null, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null)mCallback.getData(httpDataBean.getData());
                        }
                    }
                })
        );

    }

    /** 个人中心轮播图 */
    public void carouselList(final Callback mCallback){

        Map<String,String> map = new HashMap<>();

        mCompositeSubscription.add(

                mHttpGo.postSubscription("app_content/userAdList", map, null,new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null){

                                try {

                                    List<CarouselListBean> CarouselListData = JSON.parseArray(httpDataBean.getData().toString(), CarouselListBean.class);
                                    mCallback.getData(CarouselListData);

                                }catch (Exception e){

                                }
                            }
                        }
                    }
                })
        );
    }

    /** 点击广告图统计 */
    public void Statistics(String type){

        Map<String,String> map = new HashMap<>();
        map.put("type",type);

        mCompositeSubscription.add(

                mHttpGo.postSubscription("data_statistic/add", map, null,new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                    }
                })
        );

    }

    /** 退出登录 */
    public void updateUserDao(){

        mCompositeSubscription.add(new UserRx().deleteAll()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Void>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        new ToastUtil(context).getToast(false,"退出失败").show();
                    }

                    @Override
                    public void onNext(Void aVoid) {

                        new ToastUtil(context).getToast(false,"退出成功").show();

                        Intent intent = new Intent(context, SignInActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);

                    }
                })
        );
    }

    /** 注销 */
    public void remove(){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user/remove", new HashMap<String, String>(), new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        updateUserDao();
                    }
                })
        );
    }

}
